/*******************************************************************************
 * Copyright (c) 2008, 2009 Tapestry IDE Developers
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Tapestry IDE Developers - initial API and implementation
 *******************************************************************************/
package com.googlecode.tapestry.ide.internal.ui.navigator;

import org.eclipse.core.resources.IProject;

import com.googlecode.tapestry.ide.core.model.IModelElement;
import com.googlecode.tapestry.ide.core.model.ITapestryContainer;
import com.googlecode.tapestry.ide.core.model.ITapestryItem;
import com.googlecode.tapestry.ide.core.model.ITapestryModel;
import com.googlecode.tapestry.ide.core.model.ITapestryProject;

/**
 * The Class ProjectExplorerContentProvider.
 */
public class ProjectExplorerContentProvider extends
		AbstractNavigatorContentProvider {

	/**
	 * Instantiates a new project explorer content provider.
	 * 
	 * @param tapestryModel
	 *            the tapestry model
	 */
	public ProjectExplorerContentProvider(ITapestryModel tapestryModel) {
		super(tapestryModel);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @seecom.googlecode.tapestry.ide.internal.ui.navigator.
	 * AbstractNavigatorContentProvider#getChildren(java.lang.Object)
	 */
	@Override
	public Object[] getChildren(Object parentElement) {
		if (parentElement instanceof IProject) {
			ITapestryProject project = tapestryModel
					.getProject((IProject) parentElement);
			if (project != null) {
				return new Object[] { project };
			}
		}
		if (parentElement instanceof ITapestryProject) {
			ITapestryProject project = (ITapestryProject) parentElement;
			return new Object[] { project.getPages(), project.getComponents() };
		}
		if (parentElement instanceof ITapestryContainer<?>) {
			return ((ITapestryContainer<?>) parentElement).getElementChildren();
		}
		if (parentElement instanceof ITapestryItem) {
			ITapestryItem item = (ITapestryItem) parentElement;
			if (item.getTemplateResource() != null) {
				return new Object[] { item.getCompilationUnit(),
						item.getTemplateResource() };
			}
			return new Object[] { ((ITapestryItem) parentElement)
					.getCompilationUnit() };
		}
		return super.getChildren(parentElement);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @seecom.googlecode.tapestry.ide.internal.ui.navigator.
	 * AbstractNavigatorContentProvider#getParent(java.lang.Object)
	 */
	@Override
	public Object getParent(Object element) {
		if (element instanceof ITapestryProject) {
			return ((ITapestryProject) element).getProject();
		}
		if (element instanceof IModelElement) {
			return ((IModelElement) element).getElementParent();
		}
		return super.getParent(element);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @seecom.googlecode.tapestry.ide.internal.ui.navigator.
	 * AbstractNavigatorContentProvider#hasChildren(java.lang.Object)
	 */
	@Override
	public boolean hasChildren(Object element) {
		if (element instanceof IProject) {
			return true;
		}
		if (element instanceof IModelElement) {
			return true;
		}
		return super.hasChildren(element);
	}

}
